﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manager_user : System.Web.UI.Page
{
    private string TABLE_QUESTIONS = "Questions";
    private SQLQuery sqlQuery = null;
    private Connection myConnection = null;
    private string txtQuesCode, txtType, txtContent, txtAnswer;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("../index.htm");
        }
        myConnection = new Connection();
        if (!myConnection.isConnectionDatabase())
        {
            Response.Redirect("../disconnect.htm");
        }

        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {            
            fillDataToGridView();
            showGridView();
        }   
    }

    private void fillDataToGridView()
    {
        sqlQuery.loadDataGridView(mGridView, LocalVariables.SQLQUERY_SELECT_TABLE_QUESTIONS);
    }

    private void showGridView()
    {
        layout_gridview.Visible = true;
        layout_infor.Visible = false;
    }

    private void showLayoutInfor()
    {
        layout_gridview.Visible = false;
        layout_infor.Visible = true;
    }

    private bool getText()
    {
        txtQuesCode = txt_quescode.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtQuesCode))
        {
            lbl_msg.Text = LocalVariables.QUESCODE_FAILURE;
            return false;
        }
        if (ddl_type.SelectedIndex == 0)
        {
            lbl_msg.Text = LocalVariables.CHOOSE_QUES_TYPE;
            return false;
        }
        txtType = ddl_type.Text.ToString().Trim();
        txtAnswer = "";
        if (rdb_a.Checked)
        {
            txtAnswer = "A";
        }
        if (rdb_b.Checked)
        {
            txtAnswer = "B";
        }
        if (rdb_c.Checked)
        {
            txtAnswer = "C";
        }
        if (rdb_d.Checked)
        {
            txtAnswer = "D";
        }
        if (LocalVariables.isNullorEmpty(txtAnswer))
        {
            lbl_msg.Text = LocalVariables.SELECT_ANSWER;
            return false;
        }
        txtContent = ftb_content.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtContent))
        {
            lbl_msg.Text = LocalVariables.CONTENT_FAILURE;
            return false;
        }
        return true;
    }
    
    protected void mGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        showLayoutInfor();
        btn_save.Visible = false;
        btn_reset.Visible = false;
        btn_edit.Visible = true;
        txt_quescode.Enabled = false;
        txt_quescode.Text = mGridView.SelectedRow.Cells[1].Text;
        ddl_type.Text = mGridView.SelectedRow.Cells[4].Text;
        txtAnswer = mGridView.SelectedRow.Cells[3].Text.Trim();
        rdb_a.Checked = false;
        rdb_b.Checked = false;
        rdb_c.Checked = false;
        rdb_d.Checked = false;
        if (txtAnswer == "A") { rdb_a.Checked = true; }
        if (txtAnswer == "B") { rdb_b.Checked = true; }
        if (txtAnswer == "C") { rdb_c.Checked = true; }
        if (txtAnswer == "D") { rdb_d.Checked = true; }

        ftb_content.Text = sqlQuery.selectContentTableQuestions(mGridView.SelectedRow.Cells[1].Text);
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        showGridView();
    }
    
    protected void btn_add_Click(object sender, EventArgs e)
    {
        showLayoutInfor();
        txt_quescode.Enabled = true;
        btn_save.Visible = true;
        btn_reset.Visible = true;
        btn_edit.Visible = false;
        clearText();
    }

    private void clearText()
    {
        lbl_msg.Text = "";
        lbl_not_data.Text = "";
        txt_quescode.Text = "";
        ddl_type.SelectedIndex = 0;
        ftb_content.Text = "";
        rdb_a.Checked = false;
        rdb_b.Checked = false;
        rdb_c.Checked = false;
        rdb_d.Checked = false;
    }

    protected void btn_reset_Click(object sender, EventArgs e)
    {
        clearText();
    }

    protected void btn_edit_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.updateTableQuestions(txtType, txtQuesCode, txtContent, txtAnswer))
            {
                lbl_not_data.Text = LocalVariables.EDIT_QUES_SUCCESS;
                fillDataToGridView();
                showGridView();
            }
        }
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlInputCheckBox chk = new HtmlInputCheckBox();
            for (int i = 0; i < mGridView.Rows.Count; i++)
            {
                chk = (HtmlInputCheckBox)mGridView.Rows[i].FindControl("chk");
                if (chk.Checked == true)
                {
                    sqlQuery.deleteTable(TABLE_QUESTIONS, "[quesCode]", chk.Value.ToString());
                }
            }
            fillDataToGridView();
        }
        catch { }
    }

    protected void btn_refresh_Click(object sender, EventArgs e)
    {
        fillDataToGridView();
        lbl_not_data.Text = "";
        txt_search.Text = "";
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        string value = txt_search.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(value))
        {
            lbl_not_data.Text = LocalVariables.KEYWORD_FAILURE;
            return;
        }
        lbl_not_data.Text = "";
        if (sqlQuery.searchInforTableQuestions(mGridView, value))
        {
            lbl_not_data.Text = LocalVariables.SEARCH_NOT_RESULTS;
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.selectQuesCodeIsExits(TABLE_QUESTIONS, txtQuesCode))
            {
                lbl_msg.Text = LocalVariables.QCODE_BEEN_USED;
            }
            else
            {
                if (sqlQuery.insertTableQuestions(txtType, txtQuesCode.ToUpper().Trim(), txtContent, txtAnswer))
                {
                    lbl_not_data.Text = LocalVariables.ADD_QUES_SUCCESS;                   
                    fillDataToGridView();
                    showGridView();
                }
            }
        }
    }

    protected void mGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView.PageIndex = e.NewPageIndex;
        fillDataToGridView();
    }
}
